﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using Fatec.ControleEstoque.Attributes;
using Fatec.ControleEstoque.Data.Model;
using System.Data.Objects;

namespace Fatec.ControleEstoque.Interface.CustomPages.RelatorioEstoque
{
    public partial class List : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
            table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            GridView1.ColumnsGenerator = new HideColumnFieldsManager(table, PageTemplate.List);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            Title = table.DisplayName;


            if (!Page.IsPostBack)
            {
                dataFinalTextBox.Text = DateTime.Now.Date.ToShortDateString();
                LoadGrid();
            }

            // Disable various options if the table is readonly
            if (table.IsReadOnly)
            {
                GridView1.Columns[0].Visible = false;               
            }
        }

        protected void LoadGrid()
        {
            if (!string.IsNullOrEmpty(dataFinalTextBox.Text))
            {
                EstoqueEntities db = new EstoqueEntities();
                ObjectResult<Fatec.ControleEstoque.Data.Model.RelatorioEstoque> result = db.RelatorioEstoque_Get(DateTime.Now , Convert.ToDateTime(dataFinalTextBox.Text));
                GridView1.DataSource = result.ToList();
                GridView1.DataBind();
            }
        }

        protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
        {
            GridView1.PageIndex = 0;
        }

        protected void pesquisarButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
                LoadGrid();
        }
    }
}
